﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.Runtime;
using System.Configuration;
using ClientWorkflowLibrary.DTRender;
using System.IO;

namespace ClientWorkflowLibrary
{
    /// <summary>
    /// This is where the WCF dude cames in and tells us about a talk we should get cracking on.
    /// </summary>
    class RenderingCallback : IDTRenderCallback
    {
        /// <summary>
        /// The rendering client connection - so we can let folks know when we are
        /// done.
        /// </summary>
        public DTRenderClient Render { get; set; }

        public WorkflowRuntime Runtime { get; set; }

        public int DPI { get; set; }

        public double ImageQuality { get; set; }

        public string TempRenderDir { get; set; }

        public string TalkOutputDir { get; set; }

        private RenderWFManager _renderManager;

        public RenderingCallback(RenderWFManager render)
        {
            _renderManager = render;
        }

        /// <summary>
        /// We are setup so this only gets called when we are "free"
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool RenderTalk(DTRenderTalkInfo info)
        {
            ///
            /// Make sure teh temp and output directories exist. If not, try to
            /// create them. This is the sort of thign that will often fail because
            /// we don't have privs to create the required directories.
            /// 

            DirectoryInfo tempDir = new DirectoryInfo(TempRenderDir + "\\" + info.ID.ToString("00000000"));
            DirectoryInfo outputDir = new DirectoryInfo(TalkOutputDir + "\\" + info.ID.ToString("00000000"));

            try
            {
                if (!tempDir.Exists)
                {
                    tempDir.Create();
                    tempDir.Refresh();
                }
                if (!outputDir.Exists)
                {
                    outputDir.Create();
                    outputDir.Refresh();
                }
            }
            catch (Exception)
            {
                /// TODO: fix this up so we log errors!!!
                return false;
            }

            ///
            /// The parameters that are passed to the workflow
            /// 

            Dictionary<string, object> dict = new Dictionary<string, object>(){
                {"RenderCallback", Render},
                {"agendaXML", info.AgendaXML },
                {"TalkID", info.ID},
                {"TempDirectory", tempDir},
                {"MeetingOutputDirectory", outputDir},
                {"DPI", DPI},
                {"ImageQuality", ImageQuality}
            };

            ///
            /// Start the work flow. If it we get it started, let the callback know what we are up to
            /// so it can correctly maintain its state.
            /// 

            try
            {
                WorkflowInstance instance = Runtime.CreateWorkflow(typeof(RenderSingleConference), dict);
                instance.Start();
                _renderManager.RenderStarted();
            }
            catch (Exception e)
            {
                /// TODO: Print out the error message to a log!
                return false;
            }
            return true;
        }
    }
}
